//
//  CedarTabBarController.m
//  CedarFinancePH
//
//  Created by menchao on 2018/3/19.
//  Copyright © 2018年 cedarhd. All rights reserved.
//

#import "CedarTabBarController.h"
#import "HomeViewController.h"
#import "ProductVC2.h"
#import "MineVC.h"
#import "NewsDiscoveryVC.h"

@interface CedarTabBarController ()<UITabBarControllerDelegate,UITabBarDelegate>



@end

@implementation CedarTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate=self;
   
    HomeViewController *homeVC = [[HomeViewController alloc] init];
    [self addChildController:homeVC title:@"首页" imageName:@"Home" selectedImageName:@"HomeSel" navVc:[UINavigationController class]];

    ProductVC2 *productVC = [[ProductVC2 alloc] init];
    [self addChildController:productVC title:@"产品" imageName:@"Product" selectedImageName:@"ProductSel" navVc:[UINavigationController class]];

    NewsDiscoveryVC *discoveryVC = [[NewsDiscoveryVC alloc]init];
    [self addChildController:discoveryVC title:@"发现" imageName:@"discoveryNoSel" selectedImageName:@"discoverySel" navVc:[UINavigationController class]];
    
    MineVC *mineVC =  [[MineVC alloc]init];
    [self addChildController:mineVC title:@"我的" imageName:@"Mine" selectedImageName:@"MineSel" navVc:[UINavigationController class]];
    
    [self setCustomtabbar];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setCustomtabbar{
    CGRect rect = CGRectMake(0, 0, SCREEN_WIDTH, 1);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context,kSeparateLineColor.CGColor);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [self.tabBar setShadowImage:img];
    [self.tabBar setBackgroundImage:[[UIImage alloc]init]];
    self.tabBar.translucent = NO;
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    NSMutableDictionary* p = [[NSMutableDictionary alloc] init];
    [p setValue:item.title forKey:@"$title"];
    tabBar.sensorsAnalyticsViewProperties = p;
}

//- (void)showRedPoint{
//    UIImageView *dotImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"aliyunzixun@xxx.com"]];
//    
//    dotImage.backgroundColor = [UIColor clearColor];
//    
//    
//    CGRect tabFrame = self.tabBar.frame;
//    
//    CGFloat x = ceilf(0.94 * tabFrame.size.width);
//    
//    CGFloat y = ceilf(0.2 * tabFrame.size.height);
//    
//    dotImage.frame = CGRectMake(x, y, 6, 6);
//    
//    [self.tabBar addSubview:dotImage];
//}


//- (void)viewDidLayoutSubviews {
//    [super viewDidLayoutSubviews];
//    
//    for (UIView *view in self.view.subviews) {
//        if ([view isKindOfClass:[UITabBar class]]) {
//            //此处注意设置 y的值 不要使用屏幕高度 - 49 ，因为还有tabbar的高度 ，用当前tabbarController的View的高度 - 49即可
//            view.frame = CGRectMake(view.frame.origin.x, self.view.bounds.size.height-49, view.frame.size.width, 49);
//        }
//    }
//   
//}

- (void)addChildController:(UIViewController*)childController
                     title:(NSString*)title
                 imageName:(NSString*)imageName
         selectedImageName:(NSString*)selectedImageName
                     navVc:(Class)navVc{
    // childController.title = title;
    childController.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    childController.tabBarItem.title = title;
    
    childController.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    // 设置选中tabbar文字颜色
    [childController.tabBarItem setTitleTextAttributes:@{ NSForegroundColorAttributeName :kBlueColor }forState:UIControlStateSelected];
    
    UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:childController];
    [self addChildViewController:nav];
}

//- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
//    if (viewController == self.viewControllers[2]){
//        if(![[CedarDataBaseStore shareInstance]getTokenForAppConfige]){
//            [self publish:LoginForSetting_Notification data:self.selectedViewController];
//            return NO;
//        }
//    }
//    return YES;
//}

//- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
//    //set gesture yes when showViewController
//    if ([navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
//        navigationController.interactivePopGestureRecognizer.enabled = YES;
//    }
//
//    // if rootViewController, set delegate nil
//    if (navigationController.viewControllers.count == 1) {
//        navigationController.interactivePopGestureRecognizer.enabled = NO;
//        navigationController.interactivePopGestureRecognizer.delegate = nil;
//    }
//}


@end
